﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace UI
{
    public class UIUnionFightChat : MonoBehaviour
    {
        public GameObject worldChatLabel;
        public UIScrollView scrollView;
        public UnfixedGrid grid;
        List<ChatInfo> infoList = new List<ChatInfo>();
        public void Init()
        {
            if (Chat.ChatManager.Instance.fightChatInfoQue.Count != 0)
            {
                foreach (var item in Chat.ChatManager.Instance.fightChatInfoQue)
                {
                    if (infoList.Contains(item))
                    {
                        continue;
                    }
                    else
                    {
                        InitItem(item);
                        infoList.Add(item);
                    }
                }
                grid.Reposition();
                scrollView.ResetPosition();
            }
        }
        void InitItem(ChatInfo info) 
        {
            GameObject go = Instantiate(worldChatLabel) as GameObject;
            go.transform.SetParent(grid.transform);
            go.transform.localScale = Vector3.one;
            go.transform.localEulerAngles = Vector3.zero;
            string str = info.playerName + "：" + info.content;
            go.GetComponent<WorldChatItemLabel>().Init(str, info.playerId);
            worldChatLabel.GetComponent<UILabel>().text = str;
            grid.Add(go, go.GetComponent<WorldChatItemLabel>().SetHeight());
        }
        public void Refresh()
        {
            if (Chat.ChatManager.Instance.newFightChatOnTown)
            {
                Init();
                Chat.ChatManager.Instance.newFightChatOnTown = false;
            }
        }
        void Update() 
        {
            Refresh();
        }
    }
}